package tx.bj12320.process;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.identity.User;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import tx.bj12320.model.ActivityInfo;
import tx.bj12320.model.SmsInfo;
import tx.bj12320.model.TszbProcessInfo;
import tx.bj12320.model.UserInfo;
import tx.bj12320.model.enums.ActivityEnum;
import tx.bj12320.model.enums.WorkOrderTypeEnum;
import tx.bj12320.service.ActivityInfoManager;
import tx.bj12320.service.OrgInfoManager;
import tx.bj12320.service.SmsInfoManager;
import tx.bj12320.service.TszbProcessInfoManager;
import tx.bj12320.service.UserInfoManager;
import tx.bj12320.util.WebUtils;

@Service
@Transactional
public class TszbProcessAssignmentHandler implements TaskListener {
	@Autowired
	private IdentityService identityService;

	public void setIdentityService(IdentityService identityService) {
		this.identityService = identityService;
	}

	@Autowired
	private RuntimeService runtimeService;
	@Autowired
	private TaskService taskService;

	public void setRuntimeService(RuntimeService runtimeService) {
		this.runtimeService = runtimeService;
	}

	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	@Autowired
	private OrgInfoManager orgInfoManager;

	public void setOrgInfoManager(OrgInfoManager manager) {
		this.orgInfoManager = manager;
	}

	@Autowired
	private TszbProcessInfoManager tszbProcessInfoManager;

	public void setTszbProcessInfoManager(TszbProcessInfoManager manager) {
		this.tszbProcessInfoManager = manager;
	}
	@Autowired
	private SmsInfoManager smsInfoManager;
	public void setSmsInfoManager(SmsInfoManager manager) {
		this.smsInfoManager = manager;
	}
	@Autowired
	private UserInfoManager userInfoManager;
	public void setUserInfoManager(UserInfoManager manager) {
		this.userInfoManager = manager;
	}



	public void notify(DelegateTask delegateTask) {
		Long orgInfoId=Long.valueOf(delegateTask.getVariable("orgInfoId").toString());
//		System.out.println(orgInfoId);
//		TszbProcessInfo tszbProcessInfo = tszbProcessInfoManager.getById(Long
//				.valueOf(delegateTask.getExecution().getProcessBusinessKey()));
//		Long oiid = orgInfoManager.getLevel1OrgInfo(
//				tszbProcessInfo.getOrgInfo()).getOrgInfoId();
		Long oiid = orgInfoManager.getLevel1OrgInfo(orgInfoManager.getById(orgInfoId)).getOrgInfoId();
		List<User> lus = new ArrayList<User>();
		if (delegateTask.getTaskDefinitionKey().equals(
				ActivityEnum.jbmcb.getCode())) {// 局部门承办
			Long chengbanOrgInfoId = (Long) delegateTask
					.getVariable("chengbanOrgInfoId");
			// userFirstName:单位；userLastName：部门
			lus = identityService
					.createUserQuery()
					.memberOfGroup(
							delegateTask.getTaskDefinitionKey() + "_role")
					.userFirstName(oiid + "")
					.userLastName(chengbanOrgInfoId + "").list();
		} else {
			lus = identityService
					.createUserQuery()
					.memberOfGroup(
							delegateTask.getTaskDefinitionKey() + "_role")
					.userFirstName(oiid + "").list();
		}

		for (User user : lus) {
			delegateTask.addCandidateUser(user.getId());
		}

		smsNotify(delegateTask, lus);


	}

	/**
	 * 联络员承办、局联络员承办发生短信提醒，新工单提醒
	 * @param delegateTask
	 * @param lus
	 */
	private void smsNotify(DelegateTask delegateTask, List<User> lus) {
		if (delegateTask.getTaskDefinitionKey().equals(
				ActivityEnum.llycb.getCode())||delegateTask.getTaskDefinitionKey().equals(
						ActivityEnum.jllycb.getCode())) {// 联络员承办、局联络员承办发生短信提醒
			for (User user : lus) {
				UserInfo ui=userInfoManager.getById(Long.valueOf(user.getId()));
				SmsInfo si=new SmsInfo();
				si.setUserInfoId(ui.getUserInfoId());
				si.setIsGlobal(0L);
				si.setIsSent(0L);
				si.setSendDatetime(new Date());
				si.setSellPhone(ui.getSellPhone());
				si.setInfoContent("您有新工单，请登录12320投诉转办办公自动化系统查收。联系电话：64468526。");
				smsInfoManager.save(si);
			}
		}
	}

}
